<?php

/**
 * 分页操作类
 * @version 1.0
 * @author Human
 */
class paginate_c
{
    /**
     * 分页视图使用的类
     * @var string
     */
    public $view_class = "am-pagination am-pagination-centered am-text-sm";

    /**
     * 禁止使用的视图类
     * @var string
     */
    public $disable_class = "am-disabled";

    /**
     * 当前页的视图类
     * @var mixed
     */
    public $active_class = "am-active";

    /**
     * 相邻的按钮数
     * @var int
     */
    public $adjacents = 0;

    /**
     * 上一页的显示字符
     * @var string
     */
    public $prevlabel = "&laquo;";

    /**
     * 下一页的显示字符
     * @var string
     */
    public $nextlabel = "&raquo;";

    /**
     * 换页时调用的js函数 例如page_change([x]) 将替换[x]为实际页码 x为小写
     * @var string
     */
    public $change_func = "page_change([x])";

    /**
     * 分页html输出
     * @param int $page 当前页码
     * @param int $tpages 总页数
     * @return string 返回html
     */
    function paginate($page, $tpages)
    {
        $js_func = '';
        if ($tpages <= 1 || $page > $tpages) {
            return '';
        } //如果总页数为0,1 不需要显示分页栏
        $out = "<ul style=\"clear: both\" class=\"{$this->view_class}\">\n";

        // 上一页
        if ($page == 1) { //如果为第一页
            $out .= "<li class='{$this->disable_class}'><a>" . $this->prevlabel . "</a></li>\n";
        } else {
            $thisPage = $page - 1;
            $js_func = str_replace('[x]', $thisPage, $this->change_func);
            $out .= "<li><a onclick='{$js_func}'>" . $this->prevlabel . "</a></li>\n";
        }

        // first
        if ($page > ($this->adjacents + 1)) {
            $js_func = str_replace('[x]', '1', $this->change_func);
            $out .= "<li><a onclick='{$js_func}'>1</a></li>\n";
        }

        // interval
        if ($page > ($this->adjacents + 2)) {
            $out .= "...\n";
        }

        // pages
        $pmin = ($page > $this->adjacents) ? ($page - $this->adjacents) : 1;
        $pmax = ($page < ($tpages - $this->adjacents)) ? ($page + $this->adjacents) : $tpages;
        for ($i = $pmin; $i <= $pmax; $i++) {
            if ($i == $page) {
                $out .= "<li class=\"{$this->active_class}\"><a>" . $i . "</a></li>\n";
            } elseif ($i == 1) {
                $js_func = str_replace('[x]', '1', $this->change_func);
                $out .= "<li><a onclick='{$js_func}'>" . $i . "</a></li>\n";
            } else {
                $js_func = str_replace('[x]', $i, $this->change_func);
                $out .= "<li><a onclick='{$js_func}'>" . $i . "</a></li>\n";
            }
        }

        // interval
        if ($page < ($tpages - $this->adjacents - 1)) {
            $out .= "...\n";
        }

        // last
        if ($page < ($tpages - $this->adjacents)) {
            $js_func = str_replace('[x]', $tpages, $this->change_func);
            $out .= "<li><a onclick='{$js_func}'>" . $tpages . "</a></li>\n";
        }

        // next
        if ($page < $tpages) {
            $thisPage = $page + 1;
            $js_func = str_replace('[x]', $thisPage, $this->change_func);
            $out .= "<li><a onclick='{$js_func}'>" . $this->nextlabel . "</a></li>\n";
        } else {
            $out .= "<li class='{$this->disable_class}'><a>" . $this->nextlabel . "</a></li>\n";
        }

        $out .= "</ul>";

        return $out;
    }
}